<?php
require_once('../../../../conf/config.php.inc');
require_once(SYBMAS_LIB_PATH.'/liblogin.php.inc');
require_once(SYBMAS_LAYOUT_PATH.'/layout.php.inc');
require_once(SYBMAS_LIB_PATH.'/libsybmas.php.inc');
require_once(SYBMAS_LAYOUT_PATH.'/menu.php.inc');
require_once('../../lib/libtablas.php.inc');
verificar_inicio_sesion();
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css"
	href="<?php echo LAYOUT_URI?>/sybmasstyle.css">
<script src="<?php echo LAYOUT_URI?>/jquery-1.2.1.min.js"
	type="text/javascript"></script>
<script src="<?php echo LAYOUT_URI?>/menu-collapsed.js"
	type="text/javascript"></script>
<link rel="stylesheet" type="text/css"
	href="<?php echo LAYOUT_URI?>/style.css" >
<link rel="stylesheet" type="text/css"
	href="<?php echo JAVASCRIPT_URI?>/tabs/tabs.css">
<script src="<?php echo JAVASCRIPT_URI?>/tabs/tabs.js"
	type="text/javascript"></script>
<script type="text/javascript" src="<?php echo JAVASCRIPT_URI?>/commons.js">></script>
<script type="text/javascript" src="fk.js"></script>
<script src="<?php echo JAVASCRIPT_URI?>/validacionFormularios.js"
	type="text/javascript"></script>

<title>.:: Sybmas - Definicion de restricciones</title>
<!--[if lt IE 8]>
   <style type="text/css">
   li a {display:inline-block;}
   li a {display:block;}
   </style>Eliminacion de restricciones
   <![endif]-->
</head>
<body >
<script src="<?php echo JAVASCRIPT_URI?>/tooltip/wz_tooltip.js"
	type="text/javascript"></script>
	
<div id="dialog">
<div class="title">Agregar llave foranea</div>

<form action="fk2.php" method="post" target="constraints_dialog" 
	onsubmit="return verificaFormulario()">
<?php 
$dbname=false;
foreach($_GET as $key => $value){
	$$key=$value;
	echo "<input type='hidden' name='$key' value='$value'>\n";
}
?>
<h2>Seleccione los campos que formaran la llave for&aacute;nea para: <?php echo $tablename?></h2>
<?php
$cambio_bd_exitoso=true;
$sybuser_connection=start_default_sybuser_connection();
if($dbname){
	$cambio_bd_exitoso=sybase_select_db($dbname,$sybuser_connection);
}

if($cambio_bd_exitoso){
	$query="SELECT c.colid,c.name,t.name AS type,c.length,c.prec,c.scale ".		
		"FROM syscolumns c, systypes t ".
		"WHERE c.id=$id ".
		"AND c.usertype*=t.usertype";
	$result=@sybase_query($query,$sybuser_connection);
	$cname="{$constraintType}_{$tablename}_".substr(time(),-4);	
	$query="SELECT o.id,o.name,u.name AS owner ".
		"FROM sysobjects o,sysusers u ".
		"WHERE type='U' ".
		"AND o.uid=u.uid ".
		"ORDER BY name";
	$result_tables=sybase_query($query,$sybuser_connection);
?>
<table width="100%" border="1">
	<caption>Columnas</caption>
	<tbody>
		<tr>
			<td colspan="5">
				<table width="100%">
					<tr>
						<th rowspan="2">
							<img onmouseover="Tip('Proporcione el nombre de la restriccion. Si no se establece, el servidor de bases de datos generara uno automatico')" 
							onmouseout="UnTip()" src="<?php echo IMG_URI?>/help.png">
							Nombre: 
						</th>
						<td rowspan="2">
							<input type="text" name="cname" value="<?php echo $cname?>" onkeydown="return onKeyPressPermiteAlfaNumericosNoEspacios(event)" 
							maxlength="30">
						</td>
						<th>
							<img onmouseover="Tip('Seleccione la tabla a la cual la restriccion hara referencia')" 
							onmouseout="UnTip()" src="<?php echo IMG_URI?>/help.png">
							Tabla referencia
						</th>
					</tr>
					<tr>							
						<td>
							<select id="refTable" name="refTable" onchange="getColumnsByTable(this.options[this.selectedIndex].id)">
							<option></option>
							<?php 							
							while($data=sybase_fetch_array($result_tables)){
								$_owner=$data['owner'];
								$_name=$data['name'];
								?>								
								<option id="<?php echo $data['id']?>" value="<?php echo "$_owner.$_name"?>" >
								 	<?php echo $_name?>
								 </option>							
							<?php }
							sybase_free_result($result_tables);
							?>
							</select>
						</td>
					</tr>
				</table>			
			</td>			
		</tr>		
		<tr>
			<th>
				<img onmouseover="Tip('Active los checkbox para seleccionar una columna')" 
					onmouseout="UnTip()" src="<?php echo IMG_URI?>/help.png">
			</th>
			<th>colid</th>
			<th>nombre</th>
			<th>tipo</th>
			<th>
				<img onmouseover="Tip('Una vez seleccionada la tabla de referencia y activado el check box, seleccione la columna de referencia')" 
					onmouseout="UnTip()" src="<?php echo IMG_URI?>/help.png">
			</th>			
		</tr>
		
<?php 
	$firstLoop=true;
	while($data=sybase_fetch_array($result)){
			$name=$data['name'];
			$type=$data['type'];
			$length=$data['length'];
			$prec=$data['prec'];
			$scale=$data['scale'];
			
			if(isset($prec) && isset($scale)){
				$type.="($prec,$scale)";				
			}else if(isset($length)){
				$type.="($length)";
			}
?>
		<tr>
			<td><input type="checkbox" name="columns[]" onclick="enableRefColumns(this)"
			<?php echo $firstLoop ? ' checked="checked"':''?>
			value="<?php echo $name?>"></td>
			<td><?php echo $data['colid']?></td>
			<td><?php echo $name?></td>
			<td><?php echo $type?></td>
				<td>
				<select id="refColumn_<?php echo $data['name']?>" 
					class="refColumns" name="refcolumns[]"
					<?php echo $firstLoop?'':' disabled="disabled" '?> >
					<option value=""> Seleccione la tabla</option>
				</select>
			</td>
		</tr>
<?php 
		$firstLoop=false;
	}
?>
	</tbody>
</table>
<?php 
}//if($cambio_bd_exitoso)
//Cerrando conexiones a la base de datos
sybase_close($sybuser_connection);
?>
<hr>
<input type="submit" value="Continuar">
<input type="button" value="Cancelar" onclick="window.close()">
</form>
</div>
</body>
</html>